using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class EmployeeDAO
    {
        public bool CheckEmployeeAccount(string account)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.Employees.FirstOrDefault(p => p.EmployeeAccount == account) == null;
            }
        }

        public bool CheckEmployeeEmail(string email)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.Employees.FirstOrDefault(p => p.EmployeeEmail == email) == null;
            }
        }


        private bool validEmail(string newemail, string oldemail, out string err)
        {
            if (newemail != oldemail)
            {
                if (CheckEmployeeEmail(newemail))
                {
                    err = "";
                    return true;
                }
                err = "Email address is exits";
                return false;
            }
            err = "";
            return true;
        }

        public List<vEmployeeSubDetail> SelectEmployees(bool all)
        {
            using (var ctx = new BlueMDataContext())
            {
                IQueryable<vEmployeeSubDetail> query = ctx.vEmployeeSubDetails;

                if (!all)
                {
                    query = query.Where(p => p.EmployeeAvailable);
                }
                return query.ToList();
            }
        }

        public Employee SelectEmployee(int employeeID)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.Employees.First(p => p.EmployeeID == employeeID);
            }
        }

        public bool InsertEmployee(Employee employee)
        {
            using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 0, 30)))
            {
                using (var ctx = new BlueMDataContext())
                {
                    string err;
                    string acc = employee.EmployeeAccount;
                    int pfx = 1;
                    if (!CheckEmployeeAccount(acc))
                    {
                        while (!CheckEmployeeAccount(acc + pfx))
                        {
                            pfx++;
                        }
                        acc = acc + pfx;
                    }
                    employee.EmployeeAccount = acc;


                    if (!validEmail(employee.EmployeeEmail, "", out err))
                    {
                        throw new Exception(err);
                    }

                    ctx.Employees.InsertOnSubmit(employee);
                    ctx.SubmitChanges();
                }
                ts.Complete();
                return true;
            }
        }


        public bool UpdateEmployee(Employee employee)
        {
            using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 0, 30)))
            {
                string err;
                using (var ctx = new BlueMDataContext())
                {
                    Employee me = ctx.Employees.First(p => p.EmployeeID == employee.EmployeeID);
                    if (validEmail(employee.EmployeeEmail, me.EmployeeEmail, out err))
                    {
                        me.EmployeeName = employee.EmployeeName;
                        me.EmployeeSex = employee.EmployeeSex;
                        me.EmployeeBirthday = employee.EmployeeBirthday;
                        me.EmployeePosition = employee.EmployeePosition;
                        me.EmployeePhone = employee.EmployeePhone;
                        me.EmployeeEmail = employee.EmployeeEmail;
                        me.EmployeeAddress = employee.EmployeeAddress;
                        me.EmployeeCity = employee.EmployeeCity;
                        me.EmployeeCountry = employee.EmployeeCountry;
                        me.EmployeeJobTitle = employee.EmployeeJobTitle;
                        me.EmployeeImage = employee.EmployeeImage;
                        me.EmployeeAvailable = employee.EmployeeAvailable;

                        ctx.SubmitChanges();
                        ts.Complete();

                        return true;
                    }
                }
                throw new Exception(err);
            }
        }

        public bool DeactiveEmployee(int employeeID)
        {
            using (var ctx = new BlueMDataContext())
            {
                Employee employee = ctx.Employees.First(p => p.EmployeeID == employeeID);
                employee.EmployeeAvailable = false;
                employee.EmployeePosition = "";
                ctx.SubmitChanges();
            }

            return true;
        }

        public List<vEmployeeSubDetail> SelectEmployees(string employeeName)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.vEmployeeSubDetails.Where(e => e.EmployeeName.Contains(employeeName)).ToList();
            }
        }

        public Employee SelectEmployee(string employeeAccount, string employeePassword)
        {
            using (var ctx = new BlueMDataContext())
            {
                return
                    ctx.Employees.First(
                        e => e.EmployeeAccount == employeeAccount && e.EmployeePassword == employeePassword);
            }
        }
    }
}